package com.zkzx.operation.api.service.menu;

import com.zkzx.common.company.command.CompanyCommand;
import com.zkzx.common.company.model.CompanyVO;
import com.zkzx.common.global.Global;
import com.zkzx.operation.api.client.company.CompanyClient;
import com.zkzx.operation.api.mapper.MemberMapper;
import com.zkzx.operation.api.mapper.MemberRoleMapper;
import com.zkzx.operation.api.mapper.entity.Member;
import com.zkzx.operation.api.mapper.entity.MemberExample;
import com.zkzx.operation.api.mapper.entity.MemberRole;
import com.zkzx.util.data.RandomStringGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年04月30日 16时11分
 */
@Service
public class MemberService {

    @Autowired
    private CompanyClient companyClient;

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private MemberRoleMapper memberRoleMapper;

    /**
     * 根据企业id 查询初始化账号信息系
     * @param companyId 企业id
     * @return 初始化账号信息系
     */
    public Member findMemberByCompanyId(Integer companyId){
        MemberExample memberExample = new MemberExample();
        memberExample.createCriteria().andCompanyidEqualTo(companyId).andIsmainEqualTo((byte)1);
        Optional<Member> first = memberMapper.selectByExample(memberExample).stream().findFirst();
        return first.orElse(null);
    }

    /**
     * 新增 或 修改企业初始化账号信息
     * @param command 入参
     * @param companyId 企业id
     * @param userId 用户
     */
    public void add(CompanyCommand command,Integer companyId, Integer userId){

        CompanyVO vo = companyClient.getById(companyId);

        // 查询当前企业有没有初始化账号 有就走修改功能
        Member memberOld = findMemberByCompanyId(companyId);
        if (Objects.nonNull(memberOld)){
            MemberExample memberExample = new MemberExample();
            memberExample.createCriteria().andCompanyidEqualTo(companyId).andIsmainEqualTo((byte)1);
            memberOld.setCode(command.getAccountNumber());
            // 始终让手机号保持和表中关联一致
            memberOld.setMobile(vo.getMemberMobile());
            memberMapper.updateByExampleSelective(memberOld,memberExample);
           return;
        }

        // 判断当前企业信息是否反正 不完善查询查询一遍 补全信息
        if(StringUtils.isEmpty(command.getMemberMobile())) {
            command.setMemberMobile(vo.getMemberMobile());
            command.setAccountNumber(vo.getCid());
            command.setCid(vo.getCid());
        }

        Member member = new Member();
        member.setCid(command.getCid());
        member.setCompanyid(companyId);
        member.setCode(command.getAccountNumber());
        member.setName(command.getName());
        member.setPwd(RandomStringGenerator.getRandomStringByLength(10));
        member.setMobile(command.getMemberMobile());
        member.setCreatetime(new Date());
        member.setLastmodifiedtime(new Date());
        member.setCreateuser(userId);
        member.setLastmodifieduser(userId);
        member.setIsaccountnotexpired((byte)1);
        member.setIsaccountnotlocked((byte)1);
        member.setIscredentialsnonexpired((byte)1);
        member.setIsenabled((byte)1);
        member.setIsmain((byte)1);
       memberMapper.insert(member);

        Member mem = findMemberByCompanyId(companyId);
        MemberRole memberRole = new MemberRole();
        memberRole.setMemberid(mem.getId());
        memberRole.setRoleid(Global.ONE);
        memberRole.setIsmenu(1);
        memberRoleMapper.insert(memberRole);
    }

    /**
     * 重置企业初始化账号密码
     * @param companyId 企业id
     * @param userId 当前按钮
     */
    public void updatePwd(Integer companyId, Integer userId) {
        Member member = findMemberByCompanyId(companyId);
        member.setPwd(RandomStringGenerator.getRandomStringByLength(10));
        member.setLastmodifiedtime(new Date());
        member.setLastmodifieduser(userId);
        memberMapper.updateByPrimaryKey(member);
    }
}
